package com.apps.diet.dao;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.apps.diet.domain.MealNutriFactXref;
import com.apps.diet.domain.Meals;



@Repository
public class MealsDaoImpl extends AbstractDAO implements IMealsDao {

	Logger logger = Logger.getLogger(MealsDaoImpl.class);

	@SuppressWarnings("static-access")
	@Override
	public List<Meals> getMeals (String searchString) {
		SessionFactory sf = hbUtil.getSessionFactory();
        Session session = sf.openSession();

        Criteria c = session.createCriteria(Meals.class);

        if (searchString == null){
        	searchString = "";
        }
        
        c.add(Restrictions.like("mealName", "%"+searchString+"%"));

        List<Meals> mealsList = c.list();
        logger.debug("Total result found: "+mealsList.size());
        session.close();
        
        return mealsList;
	}
	
	@Override
	public boolean createMeals(Meals meals) {
		SessionFactory sf = hbUtil.getSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        try {
        	List<MealNutriFactXref> nutriFactXrefList = meals.getMealNutriFactXrefList();

        	for (Iterator<MealNutriFactXref> nutriFactXrefItr = nutriFactXrefList.iterator(); nutriFactXrefItr.hasNext(); ){
        		MealNutriFactXref nutriFactXref = nutriFactXrefItr.next();
        		nutriFactXref.setMeals(meals);
        	}
        	
        	meals.setModifyBy("admin");
        	meals.setModifyDt(new Date());
        	
        	session.save(meals);
        	session.getTransaction().commit();
        }catch(Exception e){
        	session.getTransaction().rollback();
        	logger.error(e.getMessage());
        	return false;
        }finally{
            session.close();
        }
    	return true;
	}

	@Override
	public boolean updateMeals(Meals meals) {
		SessionFactory sf = hbUtil.getSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        try {
        	List<MealNutriFactXref> nutriFactXrefList = (List<MealNutriFactXref>) meals.getMealNutriFactXrefList();

        	for (Iterator<MealNutriFactXref> nutriFactXrefItr = nutriFactXrefList.iterator(); nutriFactXrefItr.hasNext(); ){
        		MealNutriFactXref nutriFactXref = nutriFactXrefItr.next();
        		nutriFactXref.setMeals(meals);
        	}
        	
        	meals.setModifyBy("admin");
        	meals.setModifyDt(new Date());
        	
        	session.update(meals);
        	session.getTransaction().commit();
        }catch(Exception e){
        	session.getTransaction().rollback();
        	logger.error(e.getMessage());
        	return false;
        }finally{
            session.close();
        }
   	
        return true;
	}

	@Override
	public boolean deleteMeals(int mealsId) throws Exception {
		SessionFactory sf = hbUtil.getSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        try{
        	Meals Meals = (Meals) session.get(Meals.class, mealsId);
    		session.delete(Meals);
    		session.getTransaction().commit();
        }catch(Exception e){
        	session.getTransaction().rollback();
        	logger.error(e.getMessage());
        	throw e;
        }finally{
            session.close();
        }

        return true;
	}
}
